package cn.jingzhuan.fm.mvp.presenter

import cn.jingzhuan.fm.mvp.contract.FMAudioCommentContract
import cn.jingzhuan.fm.mvp.model.FMAudioCommentModel
import io.reactivex.android.schedulers.AndroidSchedulers
import javax.inject.Inject


class FMAudioCommentPresenter @Inject constructor(
        private val view: FMAudioCommentContract.View,
        private val model: FMAudioCommentModel): FMAudioCommentContract.Presenter{

    var page : Int = 1
    var limit : Int = 10

    override fun refresh(audioId: String) {
        page = 1
        getCommentList(audioId,{
            view.onRefreshed()
        })
    }

    override fun loadCommentList(audioId: String) {
        getCommentList(audioId,{ })
    }

    private fun getCommentList(audioId:String,callback: () -> Unit?) {
        model.commentList(page,limit,audioId)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                if (it.isSuccess) {
                    page++
                    callback.invoke()
                    view.onDataChange(it.response,it.response.size == limit)
                }
            }) {
                view.onDataChange(mutableListOf(),false)
            }
    }

}